﻿var Prefix = "ctl00_ctl00_ContentPlaceHolder3_ContentPlaceHolder1_";

function ShowPlayerAttributesDiv(playerId) {
    document.getElementById("attributes" + playerId).style.display = "block";
}

function HidePlayerAttributesDiv(playerId) {
    document.getElementById("attributes" + playerId).style.display = "none";
}

function ChangeCSSClass(DivId, ClassName) {
    document.getElementById(DivId).className = ClassName;
}

function PlayerInGridOnMouseOver(playerId) {
    ChangeCSSClass("draggable" + playerId, "draggableGridMouseOverStyle");
    ShowPlayerAttributesDiv(playerId);
}

function PlayerInGridOnMouseOut(playerId) {
    ChangeCSSClass("draggable" + playerId, "draggableGridStyle");
    HidePlayerAttributesDiv(playerId);
}

function AddPlayerToPlayerInDDL(PlayerId, PlayerName) {
//    var Option = document.createElement("OPTION");
//    Option.text = PlayerName;
//    Option.value = PlayerId;
//    document.getElementById(Prefix + "PlayerInDropDownList").options.add(Option);
}

function AddPlayerToPlayerOutDDL(PlayerId, PlayerName) {
//    var Option = document.createElement("OPTION");
//    Option.text = PlayerName;
//    Option.value = PlayerId;
//    document.getElementById(Prefix + "PlayerOutDropDownList").options.add(Option);
}

function RemovePlayerFromPlayerInDDL(PlayerId) {
//    var List = document.getElementById(Prefix + "PlayerInDropDownList");
//    var Length = List.options.length;
//    for (var i = Length - 1; i > 0; i--) {
//        if (List.options[i].value == PlayerId) {
//            List.selectedIndex = 0;
//            List.options.remove(i);
//        }
//    } 
}

function RemovePlayerFromPlayerOutDDL(PlayerId) {
//    var List = document.getElementById(Prefix + "PlayerOutDropDownList");
//    var Length = List.options.length;
//    for (var i = Length - 1; i > 0; i--) {
//        if (List.options[i].value == PlayerId) {
//            List.selectedIndex = 0;
//            List.options.remove(i);
//        }
//    } 
}

function InitPlayerInAndOutDDL() {
//    var PlayerId;
//    var PlayerName;
//    var Positions = new Array();
//    Positions[0] = "LeftAttack";
//    Positions[1] = "LeftCenterAttack";
//    Positions[2] = "CenterAttack";
//    Positions[3] = "RightCenterAttack";
//    Positions[4] = "RightAttack";
//    Positions[5] = "LeftMidfield";
//    Positions[6] = "LeftCenterMidfield";
//    Positions[7] = "CenterMidfield";
//    Positions[8] = "RightCenterMidfield";
//    Positions[9] = "RightMidfield";
//    Positions[10] = "LeftDefence";
//    Positions[11] = "LeftCenterDefence";
//    Positions[12] = "CenterDefence";
//    Positions[13] = "RightCenterDefence";
//    Positions[14] = "RightDefence";
//    Positions[15] = "Keeper";
//    Positions[16] = "Sub1";
//    Positions[17] = "Sub2";
//    Positions[18] = "Sub3";
//    Positions[19] = "Sub4";
//    Positions[20] = "Sub5";

//    var Option;
//    var List;
//    // add empty rows to Player in and Out DDLs
//    AddPlayerToPlayerInDDL("0", " ");
//    AddPlayerToPlayerOutDDL("0", " ");

//    // go through all positions and add correct players to the correct DDLs.
//    for (var i = 0; i <= 15; i++) {
//        // adding field players to player out DDL only
//        PlayerId = document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value;
//        if (PlayerId != 0) {
//            PlayerName = document.getElementById(Prefix + Positions[i] + "PlayerName").innerHTML;
//            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);
//        }
//    }

//    // for substitute positions - add the players player in DDL and player out DDL.
//    for (var j = 16; j <= 20; j++) {
//        PlayerId = document.getElementById(Prefix + Positions[j] + "PlayerIdHiddenField").value;
//        if (PlayerId != "0") {
//            PlayerName = document.getElementById(Prefix + Positions[j] + "PlayerName").innerHTML;
//            AddPlayerToPlayerInDDL(PlayerId, PlayerName);
//            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);
//        }
//    }
}

function FieldDivOnMouseOver(DivName) {
    var playerId = document.getElementById(Prefix + DivName + "PlayerIdHiddenField").value;
    if (playerId != "0") {
        PlayerInGridOnMouseOver(playerId)
    }
}

function FieldDivOnMouseOut(DivName) {
    var playerId = document.getElementById(Prefix + DivName + "PlayerIdHiddenField").value;
    if (playerId != "0") {
        PlayerInGridOnMouseOut(playerId)
    }
}


function GetInstructionName(InstructionId) {
    var RepeaterPrefix = "ctl00_ctl00_ContentPlaceHolder3_ContentPlaceHolder1_InstructionsRepeater_ctl0";
    var InstructionNameId = parseInt(InstructionId) - 1;
    var InstructionName = document.getElementById(RepeaterPrefix + InstructionNameId + "_InstructionName").value;
    return (InstructionName);
};

function GetPreferredActionName(PreferredActionId) {
    var RepeaterPrefix = "ctl00_ctl00_ContentPlaceHolder3_ContentPlaceHolder1_PreferredActionsRepeater_ctl0";
    var ActionNameId = parseInt(PreferredActionId) - 1;
    var ActionName = document.getElementById(RepeaterPrefix + ActionNameId + "_PreferredActionName").value;
    return (ActionName);
}

function GetPreferredActionAbbreviation(PreferredActionId) {
    var ActionName = "underfined";
    if (PreferredActionId == 1) {
        ActionName = document.getElementById(Prefix + "ShotOnGoalAbbreviationHiddenField").value;
    }
    else if (PreferredActionId >= 2 && PreferredActionId <= 4) {
        ActionName = document.getElementById(Prefix + "ShortPassAbbreviationHiddenField").value;
    }
    else if (PreferredActionId >= 5 && PreferredActionId <= 7) {
        ActionName = document.getElementById(Prefix + "LongPassAbbreviationHiddenField").value;
    }
    else if (PreferredActionId >= 8 && PreferredActionId <= 10) {
        ActionName = document.getElementById(Prefix + "DribbleAbbreviationHiddenField").value;
    }
    return (ActionName);
}

function InitPreferredActionArea(AreaName) {
    var ActionId = document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value;

    if (ActionId == "1") {
        // shot on goal action. hide arrows
        $("#" + AreaName + "ActionUpArrowImage").hide();
        $("#" + AreaName + "ActionLeftArrowImage").hide();
        $("#" + AreaName + "ActionRightArrowImage").hide();
    }
    else{
        // if not - showing the arrows
        $("#" + AreaName + "ActionUpArrowImage").show();
        $("#" + AreaName + "ActionLeftArrowImage").show();
        $("#" + AreaName + "ActionRightArrowImage").show();
    }

    // set up arrow
    if (ActionId == "2" || ActionId == "5" || ActionId == "8") {
        $("#" + AreaName + "ActionUpArrowImage").attr("src", "Images/MatchInstructions/UpArrow.png")
    }
    else {
        $("#" + AreaName + "ActionUpArrowImage").attr("src", "Images/MatchInstructions/UpArrowGrey.png")
    }

    // set left arrow
    if (ActionId == "3" || ActionId == "6" || ActionId == "9") {
        $("#" + AreaName + "ActionLeftArrowImage").attr("src", "Images/MatchInstructions/LeftArrow.png")
    }
    else {
        $("#" + AreaName + "ActionLeftArrowImage").attr("src", "Images/MatchInstructions/LeftArrowGrey.png")
    }

    // set right arrow
    if (ActionId == "4" || ActionId == "7" || ActionId == "10") {
        $("#" + AreaName + "ActionRightArrowImage").attr("src", "Images/MatchInstructions/RightArrow.png")
    }
    else {
        $("#" + AreaName + "ActionRightArrowImage").attr("src", "Images/MatchInstructions/RightArrowGrey.png")
    }

    // set Label text
    document.getElementById(AreaName + "PreferredActionLabel").innerHTML = GetPreferredActionAbbreviation(ActionId);

    // set Label title
    document.getElementById(AreaName + "PreferredActionLabel").title = GetPreferredActionName(ActionId);
    
}
function PreferredActionLableClick(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    var NewPreferredAction = "1";
    if (CurrentPreferredAction == "1") {
        NewPreferredAction = "2";
    }
    else if (CurrentPreferredAction <= 7) {
        NewPreferredAction = CurrentPreferredAction + 3;
    }
    
    document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value = NewPreferredAction;
    InitPreferredActionArea(AreaName);
}

function UpActionArrowHover(AreaName) {
    $("#" + AreaName + "ActionUpArrowImage").attr("src", "Images/MatchInstructions/UpArrow.png");
}
function UpActionArrowMouseOut(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction != 2 && CurrentPreferredAction != 5 && CurrentPreferredAction != 8) {
        $("#" + AreaName + "ActionUpArrowImage").attr("src", "Images/MatchInstructions/UpArrowGrey.png");
    }
}
function UpActionArrowClick(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction == 3 || CurrentPreferredAction == 4) {
        CurrentPreferredAction = 2;
    }
    else if (CurrentPreferredAction == 6 || CurrentPreferredAction == 7) {
        CurrentPreferredAction = 5;
    }
    else if (CurrentPreferredAction == 9 || CurrentPreferredAction == 10) {
        CurrentPreferredAction = 8;
    }
    document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value = CurrentPreferredAction;
    InitPreferredActionArea(AreaName);
}

function LeftActionArrowHover(AreaName) {
    $("#" + AreaName + "ActionLeftArrowImage").attr("src", "Images/MatchInstructions/LeftArrow.png");
}
function LeftActionArrowMouseOut(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction != 3 && CurrentPreferredAction != 6 && CurrentPreferredAction != 9) {
        $("#" + AreaName + "ActionLeftArrowImage").attr("src", "Images/MatchInstructions/LeftArrowGrey.png");
    }
}
function LeftActionArrowClick(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction == 2 || CurrentPreferredAction == 4) {
        CurrentPreferredAction = 3;
    }
    else if (CurrentPreferredAction == 5 || CurrentPreferredAction == 7) {
        CurrentPreferredAction = 6;
    }
    else if (CurrentPreferredAction == 8 || CurrentPreferredAction == 10) {
        CurrentPreferredAction = 9;
    }
    document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value = CurrentPreferredAction;
    InitPreferredActionArea(AreaName);
}

function RightActionArrowHover(AreaName) {
    $("#" + AreaName + "ActionRightArrowImage").attr("src", "Images/MatchInstructions/RightArrow.png");
}
function RightActionArrowMouseOut(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction != 4 && CurrentPreferredAction != 7 && CurrentPreferredAction != 10) {
        $("#" + AreaName + "ActionRightArrowImage").attr("src", "Images/MatchInstructions/RightArrowGrey.png");
    }
}
function RightActionArrowClick(AreaName) {
    var CurrentPreferredAction = parseInt(document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value);
    if (CurrentPreferredAction == 2 || CurrentPreferredAction == 3) {
        CurrentPreferredAction = 4;
    }
    else if (CurrentPreferredAction == 5 || CurrentPreferredAction == 6) {
        CurrentPreferredAction = 7;
    }
    else if (CurrentPreferredAction == 8 || CurrentPreferredAction == 9) {
        CurrentPreferredAction = 10;
    }
    document.getElementById(Prefix + AreaName + "PreferredActionHiddenField").value = CurrentPreferredAction;

    InitPreferredActionArea(AreaName);
}

function SwapInstruction(AreaName) {

    var Instruction = document.getElementById(Prefix + AreaName + "InstructionHiddenField").value;
    var top = $("#" + AreaName).position().top;
    var left = $("#" + AreaName).position().left;

    if (Instruction == "1") {
        $("#" + AreaName + "InstructionsImage").attr("src", "Images/MatchInstructions/InstructionsUpDownArrow.png");
        document.getElementById(Prefix + AreaName + "InstructionHiddenField").value = "2";
        $("#" + AreaName + "InstructionsImage").attr("title", GetInstructionName("2"));

        top = top - 10;
        left = left - 14;
        $("#" + AreaName).css('top', top + "px");
        $("#" + AreaName).css('left', left + "px");
        
    }
    else if (Instruction == "2") {
        $("#" + AreaName + "InstructionsImage").attr("src", "Images/MatchInstructions/InstructionsUpArrow.png");
        document.getElementById(Prefix + AreaName + "InstructionHiddenField").value = "3";
        $("#" + AreaName + "InstructionsImage").attr("title", GetInstructionName("3"));

        
        top = top - 10;
        left = left - 14;
        $("#" + AreaName).css('top', top + "px");
        $("#" + AreaName).css('left', left + "px");
    }
    else if (Instruction == "3"){
        $("#" + AreaName + "InstructionsImage").attr("src", "Images/MatchInstructions/InstructionsDownArrow.png");
        document.getElementById(Prefix + AreaName + "InstructionHiddenField").value = "1";
        $("#" + AreaName + "InstructionsImage").attr("title", GetInstructionName("1"));

        top = top + 20;
        left = left + 28;
        $("#" + AreaName).css('top', top + "px");
        $("#" + AreaName).css('left', left + "px");
    }

};

function DisableEmptyPlayersCaptainDroppables(){
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    
    for (var i=0; i<=15; i++){
        if (document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value == "0"){
            $("#" + Positions[i] + "CaptainImageDiv").droppable("option", "accept", "none");
        }
    }
};
function EnableEmptyPlayersCaptainDroppables(){
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    
    for (var i=0; i<=15; i++){
        $("#" + Positions[i] + "CaptainImageDiv").droppable("option", "accept", "*");
    }
};

function DisableEmptyPlayersSetPiecesDroppables() {
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";

    for (var i = 0; i <= 15; i++) {
        if (document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value == "0") {
            $("#" + Positions[i] + "SetPiecesTakerImageDiv").droppable("option", "accept", "none");
        }
    }
};
function EnableEmptyPlayersSetPiecesDroppables() {
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";

    for (var i = 0; i <= 15; i++) {
        $("#" + Positions[i] + "SetPiecesTakerImageDiv").droppable("option", "accept", "*");
    }
};

function CanBeCaptainOrSPTaker(AreaName){
    if (AreaName == "Sub1" || AreaName == "Sub2" || AreaName == "Sub3" || AreaName == "Sub4" || AreaName == "Sub5") {
        return false;
    }
    return true;

};

function ShowSetPiecesTaker() {
    var SetPiecesTakerId = document.getElementById(Prefix + "SetPiecesTakerHiddenField").value;
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    if (SetPiecesTakerId != 0) {
        for (var i = 0; i <= 15; i++) {
            if (SetPiecesTakerId == document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value) {
                $("#" + Positions[i] + "SetPiecesTakerImageDiv").attr("className", "SetPiecesTakerDivStyle");
            }
        } 
    }
}

function ShowCaptain() {
    var CaptainId = document.getElementById(Prefix + "CaptainHiddenField").value;
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    if (CaptainId != 0) {
        for (var i = 0; i <= 15; i++) {
            if (CaptainId == document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value) {
                $("#" + Positions[i] + "CaptainImageDiv").attr("className", "CaptainDivStyle");
            }
        }
    }
}

function PenaltiesVisible() {
    var IsVisible = document.getElementById(Prefix + "PenaltiesVisibleHiddenField").value;
    if (IsVisible == "1") {
        return true;
    }
    else {
        return false;
    }
}

function AddPlayerToPenaltyTakersList(playerId, playerName, ListName) {
    var List = document.getElementById(Prefix + ListName);
    List.options.add(new Option(playerName, playerId));
}

function RemovePlayerFromPenaltyTakersList(playerId, ListName) {
//    var List = document.getElementById(Prefix + ListName);
//    var Length = List.options.length;
//    for (var i = Length - 1; i >= 0; i--) {
//        if (List.options[i].value == playerId) {
//            if (List.options[i].selected) {
//                List.selectedIndex = 0;
//            }
//            List.options.remove(i);
//        }       
//    }
}

function InitPenaltyTakersLists() {
//    var PlayerId;
//    var PlayerName;
//    var Positions = new Array();
//    Positions[0] = "LeftAttack";
//    Positions[1] = "LeftCenterAttack";
//    Positions[2] = "CenterAttack";
//    Positions[3] = "RightCenterAttack";
//    Positions[4] = "RightAttack";
//    Positions[5] = "LeftMidfield";
//    Positions[6] = "LeftCenterMidfield";
//    Positions[7] = "CenterMidfield";
//    Positions[8] = "RightCenterMidfield";
//    Positions[9] = "RightMidfield";
//    Positions[10] = "LeftDefence";
//    Positions[11] = "LeftCenterDefence";
//    Positions[12] = "CenterDefence";
//    Positions[13] = "RightCenterDefence";
//    Positions[14] = "RightDefence";
//    Positions[15] = "Keeper";
//    Positions[16] = "Sub1";
//    Positions[17] = "Sub2";
//    Positions[18] = "Sub3";
//    Positions[19] = "Sub4";
//    Positions[20] = "Sub5";

//    var Option;
//    var List;
//    
//    for (var h = 1; h <= 11; h++) {
//        Option = document.createElement("OPTION");
//        Option.text = " ";
//        Option.value = "0";
//        List = document.getElementById(Prefix + "PenaltyDropDownList" + h);
//        List.options.add(Option);
//    }
//    
//    for (var i = 0; i <= 20; i++) {
//        PlayerId = document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value;
//        if (PlayerId != "0") {
//            PlayerName = document.getElementById(Prefix + Positions[i] + "PlayerName").innerHTML;
//            ShowPlayerInPenaltiesArea(PlayerId, PlayerName);
//        }
//    }
//    var SelectedPlayer;
//    var ListLength;
//    for (var j = 1; j <= 11; j++) {
//        List = document.getElementById(Prefix + "PenaltyDropDownList" + j);
//        SelectedPlayer = document.getElementById(Prefix + "PenaltyTaker" + j + "HiddenField").value;
//        ListLength = List.options.length;
//                   
//        for (var k = 0; k < List.options.length; k++) {
//            if (List.options[k].value == SelectedPlayer) {
//                List.options[k].selected = true;
//            } 
//        }
//    }
}

function ShowPlayerInPenaltiesArea(PlayerId, playerName) {
//    for (var i = 1; i <= 11; i++) {
//        AddPlayerToPenaltyTakersList(PlayerId, playerName, "PenaltyDropDownList" + i);
//    }
}

function HidePlayerFromPenaltiesArea(PlayerId) {
//    
//    for (var i = 1; i <= 11; i++) {
//        RemovePlayerFromPenaltyTakersList(PlayerId, "PenaltyDropDownList" + i);
//    }
}

function PenaltyDDLoptionChange(ListName) {
    var CurrentListName;
    var List = document.getElementById(ListName);
    for (var i = 1; i <= 11; i++) {
        if (ListName != (Prefix + "PenaltyDropDownList" + i)) {
            CurrentList = document.getElementById(Prefix + "PenaltyDropDownList" + i);
            if ((CurrentList.selectedIndex == List.selectedIndex) && (List.selectedIndex != 0)) {
                CurrentList.selectedIndex = 0;
            }
        }
        else {
            document.getElementById(Prefix + "PenaltyTaker" + i + "HiddenField").value = List.options[List.selectedIndex].value;
        }
    }

}

function SetDefaultImageTitle(PlayerId) {
    document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "NotPlayingHiddenField").value;
}

function UpdatePlayerPositionImage(PlayerId) {
    var playerPosition = 100;
    var imageName;
    
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    Positions[16] = "Sub1";
    Positions[17] = "Sub2";
    Positions[18] = "Sub3";
    Positions[19] = "Sub4";
    Positions[20] = "Sub5";

    for (var i = 0; i <= 20; i++) {
        if (PlayerId == document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value) {
            playerPosition = i;
            break;
        }
    }
        
    // update title
    switch (playerPosition) {
        case 0:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Forward.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "ForwardNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "99";
            break;

        case 1:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Forward.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "ForwardNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "98"
            break;
        case 2:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Forward.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "CenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "ForwardNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "97"
            break;
        case 3:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Forward.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "ForwardNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "96"
            break;
        case 4:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Forward.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "ForwardNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "95"
            break;
        case 5:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Midfielder.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "MidfieldNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "94"
            break;
        case 6:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Midfielder.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "MidfieldNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "93"
            break;
        case 7:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Midfielder.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "CenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "MidfieldNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "92"
            break;
        case 8:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Midfielder.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "MidfieldNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "91"
            break;
        case 9:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Midfielder.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "MidfieldNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "90"
            break;
        case 10:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Defender.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "DefenderNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "89"
            break;
        case 11:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Defender.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "LeftCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "DefenderNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "88"
            break;
        case 12:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Defender.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "CenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "DefenderNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "87"
            break;
        case 13:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Defender.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightCenterNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "DefenderNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "86"
            break;
        case 14:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Defender.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "RightNameHiddenField").value + " " +
                                                                            document.getElementById(Prefix + "DefenderNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "85"
            break;
        case 15:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Keeper.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "KeeperNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "84"
            break;
        case 16:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Substitute.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "SubstituteNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "83"
            break;
        case 17:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Substitute.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "SubstituteNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "82"
            break;
        case 18:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Substitute.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "SubstituteNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "81"
            break;
        case 19:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Substitute.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "SubstituteNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "80"
            break;
        case 20:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/Substitute.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "SubstituteNameHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "79"
            break;
        case 100:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/NotPlaying.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "NotPlayingHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "00"
            break;
        default:
            document.getElementById("PlayerPositionImg" + PlayerId).src = "Images/MatchInstructions/NotPlaying.png";
            document.getElementById("PlayerPositionImg" + PlayerId).title = document.getElementById(Prefix + "NotPlayingHiddenField").value;
            document.getElementById("SortLabel" + PlayerId).innerHTML = "00"
            break;


    }
    $("#" + Prefix + "PlayersGridView").trigger("update"); 
}


function ClearAll() {
    if (confirm(document.getElementById(Prefix + "ClearAllQuestionText").value)) {

        var PlayerId;
        var Positions = new Array();
        Positions[0] = "LeftAttack";
        Positions[1] = "LeftCenterAttack";
        Positions[2] = "CenterAttack";
        Positions[3] = "RightCenterAttack";
        Positions[4] = "RightAttack";
        Positions[5] = "LeftMidfield";
        Positions[6] = "LeftCenterMidfield";
        Positions[7] = "CenterMidfield";
        Positions[8] = "RightCenterMidfield";
        Positions[9] = "RightMidfield";
        Positions[10] = "LeftDefence";
        Positions[11] = "LeftCenterDefence";
        Positions[12] = "CenterDefence";
        Positions[13] = "RightCenterDefence";
        Positions[14] = "RightDefence";
        Positions[15] = "Keeper";
        Positions[16] = "Sub1";
        Positions[17] = "Sub2";
        Positions[18] = "Sub3";
        Positions[19] = "Sub4";
        Positions[20] = "Sub5";

        // go through all areas and clear them
        for (var i = 0; i <= 20; i++) {
            PlayerId = document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value;
            if (PlayerId != "0") {
                // hide player
                document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value = 0;
                document.getElementById(Prefix + Positions[i] + "InjuredHiddenField").value = "false";
                document.getElementById(Prefix + Positions[i] + "PlayerName").innerHTML = "";
                $("#" + Positions[i] + "Dragable").draggable("option", "playerId", 0);
                $("#" + Positions[i] + "Dragable").draggable("option", "playerName", "");
                $("#" + Positions[i] + "Dragable").draggable("option", "IsInjured", "false");
                // close divs:
                $("#" + Positions[i] + "Dragable").hide(200);
                $("#" + Positions[i] + "Instructions").hide(200);

                $("#" + Positions[i]).removeClass("DroppableDivStyle");
                $("#" + Positions[i]).addClass("DroppableDivStyleNoBG"); 

                // set default image in the grid
                UpdatePlayerPositionImage(PlayerId);
            }
        }
        // clear SetPieces and Captain
        RemoveCaptains();
        RemoveSetPiecesTakers();
        document.getElementById(Prefix + "SetPiecesTakerHiddenField").value = "0";
        document.getElementById(Prefix + "CaptainHiddenField").value = "0";

        // clear penalty takers
        for (var j = 1; j <= 11; j++) {
            document.getElementById(Prefix + "PenaltyTaker" + j + "HiddenField").value = "0";
            document.getElementById(Prefix + "PenaltyDropDownList" + j).selectedIndex = 0;
        }
        // clear substitutions
        for (var k = 1; k <= 20; k++) {
            document.getElementById(SubPrefix + "Sub" + k + "TypeHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "TimeMinHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "TimeMaxHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "PossessionMinHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "PossessionMaxHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "ScoreMinHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "ScoreMaxHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "PlayerOutHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "PlayerInHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "NewPlayerPositionHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "NewPlayerInstructionHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "NewPlayerPreferredActionHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "NewTeamAttitudeHiddenField").value = "0";
            document.getElementById(SubPrefix + "Sub" + k + "NewTeamAggressivenessHiddenField").value = "0";

            document.getElementById(SubPrefix + "Sub" + k + "TimeLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "PossessionLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "ScoreLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "PlayerOutLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "PlayerInLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "NewPositionLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "NewInstructionLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "NewPreferredActionLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "NewAttitudeLabel").innerHTML = " ";
            document.getElementById(SubPrefix + "Sub" + k + "NewAggressivenessLabel").innerHTML = " ";

            $("#Substitution" + k).hide();
        }
        ShowFormation();
        document.getElementById(Prefix + "NumPlayersHiddenField").value = 0;
        
    }
}
//function ShowPenaltyGridviewPlayersOnInit() {
//    var PlayerId;
//    var Positions = new Array();
//    Positions[0] = "LeftAttack";
//    Positions[1] = "LeftCenterAttack";
//    Positions[2] = "CenterAttack";
//    Positions[3] = "RightCenterAttack";
//    Positions[4] = "RightAttack";
//    Positions[5] = "LeftMidfield";
//    Positions[6] = "LeftCenterMidfield";
//    Positions[7] = "CenterMidfield";
//    Positions[8] = "RightCenterMidfield";
//    Positions[9] = "RightMidfield";
//    Positions[10] = "LeftDefence";
//    Positions[11] = "LeftCenterDefence";
//    Positions[12] = "CenterDefence";
//    Positions[13] = "RightCenterDefence";
//    Positions[14] = "RightDefence";
//    Positions[15] = "Keeper";
//    Positions[16] = "Sub1";
//    Positions[17] = "Sub2";
//    Positions[18] = "Sub3";
//    Positions[19] = "Sub4";
//    Positions[20] = "Sub5";

//    for (var i = 0; i <= 20; i++) {
//        PlayerId = document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value;
//        if (PlayerId != "0") {
//            ShowPenaltyPlayersGridRow(PlayerId);
//        }
//    }

//    //Now hiding players that are already in the list 
//    for (var j = 1; j <= 11; j++) {
//        
//        PlayerId = document.getElementById(Prefix + "PenaltyTaker" + j + "IdHiddenField").value;
//        if (PlayerId != "0") {
//            HidePenaltyPlayersGridRow(PlayerId);
//        }
//    }

//}

//function ShowPenaltyTakerInNewPosition(PenaltyNum, PlayerId, PlayerName) {
//    document.getElementById(Prefix + "PenaltyTaker" + PenaltyNum + "IdHiddenField").value = PlayerId;
//    document.getElementById(Prefix + "PenaltyTaker" + PenaltyNum + "NameLabel").innerHTML = PlayerName;
//    $("#PenaltyTaker" + PenaltyNum + "Draggable").draggable("option", "playerId", PlayerId);
//    $("#PenaltyTaker" + PenaltyNum + "Draggable").draggable("option", "playerName", PlayerName);
//    var AreaName = $("#PenaltyTaker" + PenaltyNum + "Draggable").draggable("option", "DivName");
//    $(AreaName).fadeIn(200);
//}

//function HidePenaltyPlayersGridRow(PlayerId) {
//    $("#Penalty" + PlayerId).hide();
//    $("#PenExperience" + PlayerId).hide();
//    $("#PenTechnique" + PlayerId).hide();
//    $("#PenShooting" + PlayerId).hide();
//}
//function ShowPenaltyPlayersGridRow(PlayerId) {
//    $("#Penalty" + PlayerId).fadeIn(2000);
//    $("#PenExperience" + PlayerId).fadeIn(2000);
//    $("#PenTechnique" + PlayerId).fadeIn(2000);
//    $("#PenShooting" + PlayerId).fadeIn(2000);
//}

//function HidePenaltyDraggable(draggableNum) {
//    document.getElementById(Prefix + "PenaltyTaker" + draggableNum + "IdHiddenField").value = "0";
//    document.getElementById(Prefix + "PenaltyTaker" + draggableNum + "NameLabel").innerHTML = "";
//    $("#PenaltyTaker" + draggableNum + "Draggable").draggable("option", "playerId", "0");
//    $("#PenaltyTaker" + draggableNum + "Draggable").draggable("option", "playerName", "");
//    $("#PenaltyTaker" + draggableNum + "Draggable").hide();
//}

//function PenaltyDroppableDrop(event, ui, Num) {
//    var droppedPlayerId = $(ui.draggable).draggable("option", "playerId");
//    var droppedPlayerName = $(ui.draggable).draggable("option", "playerName");
//    var DivName = $(ui.draggable).draggable("option", "DivName");
//    var droppedPenaltyNum = $(ui.draggable).draggable("option", "PenaltyNum");
//    var CurrentPlayerId = document.getElementById(Prefix + "PenaltyTaker" + Num + "IdHiddenField").value;
//    var CurrentPlayerName = document.getElementById(Prefix + "PenaltyTaker" + Num + "NameLabel").innerHTML;

//    // show the player here
//    ShowPenaltyTakerInNewPosition(Num, droppedPlayerId, droppedPlayerName);

//    if (DivName == "GridView") {
//        // hide the player there
//        HidePenaltyPlayersGridRow(droppedPlayerId);

//        // show row of the current player there:
//        if (CurrentPlayerId != "0") {
//            ShowPenaltyPlayersGridRow(CurrentPlayerId)
//        }
//    }
//    else {
//        if (CurrentPlayerId != "0") {
//            ShowPenaltyTakerInNewPosition(droppedPenaltyNum, CurrentPlayerId, CurrentPlayerName);
//        }
//        else {
//            HidePenaltyDraggable(droppedPenaltyNum);
//        }
//    }
//}

function ShowPlayerRow(PlayerId) {
    UpdatePlayerPositionImage(PlayerId);
//    $("#draggable" + PlayerId).show(200);
//    $("#fitness" + PlayerId).show(200);
//    $("#Note" + PlayerId).show(200);
//    $("#experience" + PlayerId).show(200);
//    $("#Number" + PlayerId).show(200);
//    $("#playerGridPosition" + PlayerId).show(200);
//    $("#Age" + PlayerId).show(200);
//    $("#Charisma" + PlayerId).show(200);
//    $("#StrongFoot" + PlayerId).show(200);
//    $("#Stamina" + PlayerId).show(200);
//    $("#Instincts" + PlayerId).show(200);
//    $("#GoalKeeping" + PlayerId).show(200);
//    $("#Positioning" + PlayerId).show(200);
//    $("#Power" + PlayerId).show(200);
//    $("#Tackling" + PlayerId).show(200);
//    $("#Heading" + PlayerId).show(200);
//    $("#Quickness" + PlayerId).show(200);
//    $("#Technique" + PlayerId).show(200);
//    $("#ShortPassing" + PlayerId).show(200);
//    $("#LongPassing" + PlayerId).show(200);
//    $("#Shooting" + PlayerId).show(200);

}

function IsSubDraggable(DragType)
{
    if ((DragType == "#Sub1Dragable") ||
        (DragType == "#Sub2Dragable") ||
        (DragType == "#Sub3Dragable") ||
        (DragType == "#Sub4Dragable") ||
        (DragType == "#Sub5Dragable") ||
        (DragType == "#KeeperDragable")) {
        return true;
    }
    else {
        return false;
    }
}

function IsTrueSubDraggable(DragType) {
    if ((DragType == "#Sub1Dragable") ||
        (DragType == "#Sub2Dragable") ||
        (DragType == "#Sub3Dragable") ||
        (DragType == "#Sub4Dragable") ||
        (DragType == "#Sub5Dragable")) {
        return true;
    }
    else {
        return false;
    }
}


function ShowFormation() {
    var Defenders = 0;
    var Midfielders = 0;
    var Forwards = 0;
    var Postfix = "PlayerIdHiddenField";
    if (document.getElementById(Prefix + "LeftAttack" + Postfix).value != "0")
        Forwards++;
    if (document.getElementById(Prefix + "LeftCenterAttack" + Postfix).value != "0")
        Forwards++;
    if (document.getElementById(Prefix + "CenterAttack" + Postfix).value != "0")
        Forwards++;
    if (document.getElementById(Prefix + "RightCenterAttack" + Postfix).value != "0")
        Forwards++;
    if (document.getElementById(Prefix + "RightAttack" + Postfix).value != "0")
        Forwards++;
    if (document.getElementById(Prefix + "LeftMidfield" + Postfix).value != "0")
        Midfielders++;
    if (document.getElementById(Prefix + "LeftCenterMidfield" + Postfix).value != "0")
        Midfielders++;
    if (document.getElementById(Prefix + "CenterMidfield" + Postfix).value != "0")
        Midfielders++;
    if (document.getElementById(Prefix + "RightCenterMidfield" + Postfix).value != "0")
        Midfielders++;
    if (document.getElementById(Prefix + "RightMidfield" + Postfix).value != "0")
        Midfielders++;
    if (document.getElementById(Prefix + "LeftDefence" + Postfix).value != "0")
        Defenders++;
    if (document.getElementById(Prefix + "LeftCenterDefence" + Postfix).value != "0")
        Defenders++;
    if (document.getElementById(Prefix + "CenterDefence" + Postfix).value != "0")
        Defenders++;
    if (document.getElementById(Prefix + "RightCenterDefence" + Postfix).value != "0")
        Defenders++;
    if (document.getElementById(Prefix + "RightDefence" + Postfix).value != "0")
        Defenders++;

    document.getElementById(Prefix + "FormationLabel").innerHTML = Defenders + " - " + Midfielders + " - " + Forwards;
}

function HidePlayerRow(PlayerId) {
    UpdatePlayerPositionImage(PlayerId);
//    $("#draggable" + PlayerId).hide();
//    $("#fitness" + PlayerId).hide();
//    $("#Note" + PlayerId).hide();
//    $("#experience" + PlayerId).hide();
//    $("#Number" + PlayerId).hide();
//    $("#playerGridPosition" + PlayerId).hide();
//    $("#Age" + PlayerId).hide();
//    $("#Charisma" + PlayerId).hide();
//    $("#StrongFoot" + PlayerId).hide();
//    $("#Stamina" + PlayerId).hide();
//    $("#Instincts" + PlayerId).hide();
//    $("#GoalKeeping" + PlayerId).hide();
//    $("#Positioning" + PlayerId).hide();
//    $("#Power" + PlayerId).hide();
//    $("#Tackling" + PlayerId).hide();
//    $("#Heading" + PlayerId).hide();
//    $("#Quickness" + PlayerId).hide();
//    $("#Technique" + PlayerId).hide();
//    $("#ShortPassing" + PlayerId).hide();
//    $("#LongPassing" + PlayerId).hide();
//    $("#Shooting" + PlayerId).hide();
}

function CheckAndAddElevenPlayers() {
    var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
    var NumPlayers = parseInt(HiddneField.value);
    if (NumPlayers >= 10) {
        return false;
    }
    HiddneField.value = NumPlayers + 1;
    return true;
}

function HidePlayerIfExists(PlayerId, AreaName) {
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    Positions[16] = "Sub1";
    Positions[17] = "Sub2";
    Positions[18] = "Sub3";
    Positions[19] = "Sub4";
    Positions[20] = "Sub5";   
     
    var LocalPlayerId;
    for (var i = 0; i <= 20; i++){
        if ((Positions[i] != AreaName) && (document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value == PlayerId)) {
            // player should be removed 
            document.getElementById(Prefix + Positions[i] + "PlayerIdHiddenField").value = 0;
            document.getElementById(Prefix + Positions[i] + "InjuredHiddenField").value = "false";
            document.getElementById(Prefix + Positions[i] + "PlayerName").innerHTML = "";
            $("#" + Positions[i] + "Dragable").draggable("option", "playerId", 0);
            $("#" + Positions[i] + "Dragable").draggable("option", "playerName", "");
            $("#" + Positions[i] + "Dragable").draggable("option", "IsInjured", "false");
            $("#" + Positions[i]).removeClass("DroppableDivStyle");
            $("#" + Positions[i]).addClass("DroppableDivStyleNoBG");
            
            
            // close divs there:
            $("#" + Positions[i] + "Dragable").hide(200);
            $("#" + Positions[i] + "Instructions").hide(200);

            // close captain and set pieces taker there
            $("#" + Positions[i] + "CaptainImageDiv").attr("class", "EmptyCaptainSetPiecesStyle");
            $("#" + Positions[i] + "SetPiecesTakerImageDiv").attr("class", "EmptyCaptainSetPiecesStyle");

            // substract number of players if the player is not 
            if (i < 15) {
                var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
                HiddneField.value = parseInt(HiddneField.value) - 1;
            }
        }
    }
}


function DisableFieldDroppables() {
    $("#LeftAttack").droppable("option", "accept", "none");
    $("#LeftCenterAttack").droppable("option", "accept", "none");
    $("#CenterAttack").droppable("option", "accept", "none");
    $("#RightCenterAttack").droppable("option", "accept", "none");
    $("#RightAttack").droppable("option", "accept", "none");
    $("#LeftMidfield").droppable("option", "accept", "none");
    $("#LeftCenterMidfield").droppable("option", "accept", "none");
    $("#CenterMidfield").droppable("option", "accept", "none");
    $("#RightCenterMidfield").droppable("option", "accept", "none");
    $("#RightMidfield").droppable("option", "accept", "none");
    $("#LeftDefence").droppable("option", "accept", "none");
    $("#LeftCenterDefence").droppable("option", "accept", "none");
    $("#CenterDefence").droppable("option", "accept", "none");
    $("#RightCenterDefence").droppable("option", "accept", "none");
    $("#RightDefence").droppable("option", "accept", "none");
    $("#Keeper").droppable("option", "accept", "none");
    $("#Sub1").droppable("option", "accept", "none");
    $("#Sub2").droppable("option", "accept", "none");
    $("#Sub3").droppable("option", "accept", "none");
    $("#Sub4").droppable("option", "accept", "none");
    $("#Sub5").droppable("option", "accept", "none");
}

function EnableFieldDroppables() {
    $("#LeftAttack").droppable("option", "accept", "*");
    $("#LeftCenterAttack").droppable("option", "accept", "*");
    $("#CenterAttack").droppable("option", "accept", "*");
    $("#RightCenterAttack").droppable("option", "accept", "*");
    $("#RightAttack").droppable("option", "accept", "*");
    $("#LeftMidfield").droppable("option", "accept", "*");
    $("#LeftCenterMidfield").droppable("option", "accept", "*");
    $("#CenterMidfield").droppable("option", "accept", "*");
    $("#RightCenterMidfield").droppable("option", "accept", "*");
    $("#RightMidfield").droppable("option", "accept", "*");
    $("#LeftDefence").droppable("option", "accept", "*");
    $("#LeftCenterDefence").droppable("option", "accept", "*");
    $("#CenterDefence").droppable("option", "accept", "*");
    $("#RightCenterDefence").droppable("option", "accept", "*");
    $("#RightDefence").droppable("option", "accept", "*");
    $("#Keeper").droppable("option", "accept", "*");
    $("#Sub1").droppable("option", "accept", "*");
    $("#Sub2").droppable("option", "accept", "*");
    $("#Sub3").droppable("option", "accept", "*");
    $("#Sub4").droppable("option", "accept", "*");
    $("#Sub5").droppable("option", "accept", "*");
}

function RemoveCaptains(){
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";

    for (var i = 0; i <= 15; i++) {
        $("#" + Positions[i] + "CaptainImageDiv").attr("class", "EmptyCaptainSetPiecesStyle");
    }
}
function RemoveSetPiecesTakers(){
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    
    for (var i=0; i<= 15; i++){
        $("#" + Positions[i] + "SetPiecesTakerImageDiv").attr("class", "EmptyCaptainSetPiecesStyle");
    }
}

function FieldDroppableDrop(PositionName, event, ui) {
    var CurrentPlayerId = $("#" + PositionName + "Dragable").draggable("option", "playerId");
    var CurrentPlayerName = $("#" + PositionName + "Dragable").draggable("option", "playerName");
    var CurrentInjured = $("#" + PositionName + "Dragable").draggable("option", "IsInjured");
    var PlayerId = $(ui.draggable).draggable("option", "playerId");
    var Captain = document.getElementById(Prefix + "CaptainHiddenField").value;
    var SetPiecesTaker = document.getElementById(Prefix + "SetPiecesTakerHiddenField").value;
    var PlayerName = $(ui.draggable).draggable("option", "playerName");
    var IsInjured = $(ui.draggable).draggable("option", "IsInjured");
    var DragType = $(ui.draggable).draggable("option", "DragType");
    var AreaName;
    
    if (DragType == "Gridview") {
        HidePlayerIfExists(PlayerId, PositionName);
    }
    
    // check that drop is legal - cannot be more than 11 players on the field.
    if (((DragType == "Gridview") || (IsSubDraggable(DragType))) &&
         (CurrentPlayerId == "0") && (PositionName != "Keeper")) {
        if (!CheckAndAddElevenPlayers()) {
            alert(document.getElementById(Prefix + "ElevenPlayersMessageHiddenField").value);
            return false;
        }
    }

    if (PositionName == "Keeper" && DragType != "Gridview" && (!IsSubDraggable(DragType))) {
        var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
        HiddneField.value = parseInt(HiddneField.value) - 1;
    }


    // show dropped player here
    document.getElementById(Prefix + PositionName + "PlayerIdHiddenField").value = PlayerId;
    document.getElementById(Prefix + PositionName + "InjuredHiddenField").value = IsInjured;
    document.getElementById(Prefix + PositionName + "PlayerName").innerHTML = PlayerName;
    $("#" + PositionName + "Dragable").draggable("option", "playerId", PlayerId);
    $("#" + PositionName + "Dragable").draggable("option", "playerName", PlayerName);
    $("#" + PositionName + "Dragable").draggable("option", "IsInjured", IsInjured);
    $("#" + PositionName).removeClass("DroppableDivStyleNoBG");
    $("#" + PositionName).addClass("DroppableDivStyle");


    // read draggable Area name - will be needed for operations 
    // when other draggable from droppable is dropped.
    AreaName = $(ui.draggable).draggable("option", "AreaName");
    if (CurrentPlayerId != "0") {
        if (DragType == "Gridview") {
            // show current player in the gridview
            ShowPlayerRow(CurrentPlayerId);

            // hide dropped player from the gridview
            HidePlayerRow(PlayerId);

            // Show dropped player in the Penalties area
            ShowPlayerInPenaltiesArea(PlayerId, PlayerName);
            
            // hide previous player from the penalties area
            HidePlayerFromPenaltiesArea(CurrentPlayerId);

            // show player in Sub playerOutDDL
            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);

            // remove previous player from PlayerOut DDL:
            RemovePlayerFromPlayerOutDDL(CurrentPlayerId);

            if ((Captain != "0") && (CurrentPlayerId == Captain)) {
                // gridview player subs this player, means that captain is now undefined
                document.getElementById(Prefix + "CaptainHiddenField").value = 0;
                RemoveCaptains();
            }
            if ((SetPiecesTaker != "0" ) && (CurrentPlayerId == SetPiecesTaker)) {
                // gridview player subs this player - means that Set pieces taker is now undefined
                document.getElementById(Prefix + "SetPiecesTakerHiddenField").value = 0;
                RemoveSetPiecesTakers();
            }

            // if the player droppes is the captain
            if (Captain == PlayerId) {
                $("#" + PositionName + "CaptainImageDiv").attr("class", "CaptainDivStyle");
            }
            if (SetPiecesTaker == PlayerId) {
                $("#" + PositionName + "SetPiecesTakerImageDiv").attr("class", "SetPiecesTakerDivStyle");
            }
        }
        else {
            // swap happens:
            // show current player there
            document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value = CurrentPlayerId;
            document.getElementById(Prefix + AreaName + "InjuredHiddenField").value = CurrentInjured;
            document.getElementById(Prefix + AreaName + "PlayerName").innerHTML = CurrentPlayerName;
            $(ui.draggable).draggable("option", "playerId", CurrentPlayerId);
            $(ui.draggable).draggable("option", "playerName", CurrentPlayerName);
            $(ui.draggable).draggable("option", "IsInjured", CurrentInjured);

            // deal with captain:
            if (Captain != "0") {
                if (CurrentPlayerId == Captain) {
                    if (CanBeCaptainOrSPTaker(AreaName)) {
                        // this player was captain. he stais the captain, but in his new place:
                        RemoveCaptains();
                        $("#" + AreaName + "CaptainImageDiv").attr("class", "CaptainDivStyle");
                    }
                    else {
                        // dropped a substitute here. Means that current captain becomes substitute - and needs to be deleted
                        RemoveCaptains();
                        document.getElementById(Prefix + "CaptainHiddenField").value = "0";
                    }
                }
                if (PlayerId == Captain) {
                    // dropped player is the captain. He stays the captain, but need to show it here
                    RemoveCaptains();
                    $("#" + PositionName + "CaptainImageDiv").attr("class", "CaptainDivStyle");
                }
            }

            // deal with set pieces takers:
            if (SetPiecesTaker != "0") {
                if (CurrentPlayerId == SetPiecesTaker) {
                    if (CanBeCaptainOrSPTaker(AreaName)) {
                        // This player is set pieces taker, He stays one but in the other place
                        RemoveSetPiecesTakers();
                        $("#" + AreaName + "SetPiecesTakerImageDiv").attr("class", "SetPiecesTakerDivStyle");
                    }
                    else {
                        // Substitute player was dropped here. As the player cannot continue being SP taker when he is a sub - SP taker is initialized
                        RemoveSetPiecesTakers();
                        document.getElementById(Prefix + "SetPiecesTakerHiddenField").value = "0";
                    }
                }
                if (PlayerId == SetPiecesTaker) {
                    // dropped player is the set pieces taker. He stays one, but here
                    RemoveSetPiecesTakers();
                    $("#" + PositionName + "SetPiecesTakerImageDiv").attr("class", "SetPiecesTakerDivStyle");
                }
            }

            // if dropped a Substitue here - deal with Substitution DDLs:
            if (IsTrueSubDraggable(DragType)) {
                AddPlayerToPlayerInDDL(CurrentPlayerId, CurrentPlayerName);
                RemovePlayerFromPlayerInDDL(PlayerId);
            }

            // update grid rows
            UpdatePlayerPositionImage(PlayerId);
            UpdatePlayerPositionImage(CurrentPlayerId);
        }

    }
    else {
        if (DragType == "Gridview") {
            //hide dropped player from the gridview
            HidePlayerRow(PlayerId);

            // Show dropped player in the Penalties area
            ShowPlayerInPenaltiesArea(PlayerId, PlayerName);

            // Add player to player Out DDL
            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);

            // if the player droppes is the captain
            if (Captain == PlayerId) {
                $("#" + PositionName + "CaptainImageDiv").attr("class", "CaptainDivStyle");
            }
            if (SetPiecesTaker == PlayerId) {
                $("#" + PositionName + "SetPiecesTakerImageDiv").attr("class", "SetPiecesTakerDivStyle");
            }
        }
        else {
            // hide dropped player there
            document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value = 0;
            document.getElementById(Prefix + AreaName + "InjuredHiddenField").value = "false";
            document.getElementById(Prefix + AreaName + "PlayerName").innerHTML = "";
            $(ui.draggable).draggable("option", "playerId", 0);
            $(ui.draggable).draggable("option", "playerName", "");
            $(ui.draggable).draggable("option", "IsInjured", "false");
            
            $("#" + $(ui.draggable).draggable("option", "AreaName")).removeClass("DroppableDivStyle");
            $("#" + $(ui.draggable).draggable("option", "AreaName")).addClass("DroppableDivStyleNoBG");

            // close divs there:
            $("#" + AreaName + "Dragable").hide(200);
            $("#" + AreaName + "Instructions").hide(200);

            // deal with dropped player being captain
            if ((PlayerId == Captain) && (Captain != "0")) {
                RemoveCaptains();
                $("#" + PositionName + "CaptainImageDiv").attr("class", "CaptainDivStyle");
            }

            // deal with dropped player being SP taker
            if ((PlayerId == SetPiecesTaker) && (SetPiecesTaker != "0")) {
                RemoveSetPiecesTakers();
                $("#" + PositionName + "SetPiecesTakerImageDiv").attr("class", "SetPiecesTakerDivStyle");
            }

            // deal with substitution DDLs
            if (IsTrueSubDraggable(DragType)) {
                RemovePlayerFromPlayerInDDL(PlayerId);
            }

            // update Grid position
            UpdatePlayerPositionImage(PlayerId);
        }

        // open divs here:
        $("#" + PositionName + "Dragable").fadeIn(200);
        $("#" + PositionName + "Instructions").fadeIn(200);
    }
    ShowFormation();
    PlayerInGridOnMouseOut(PlayerId);
    if (CurrentPlayerId != "0") {
        PlayerInGridOnMouseOut(CurrentPlayerId);    
    }
}

function SubDroppableDrop(PositionName, event, ui) {
    var CurrentPlayerId = $("#" + PositionName + "Dragable").draggable("option", "playerId");
    var CurrentPlayerName = $("#" + PositionName + "Dragable").draggable("option", "playerName");
    var CurrentInjured = $("#" + PositionName + "Dragable").draggable("option", "IsInjured");
    var PlayerId = $(ui.draggable).draggable("option", "playerId");
    var PlayerName = $(ui.draggable).draggable("option", "playerName");
    var IsInjured = $(ui.draggable).draggable("option", "IsInjured");
    var DragType = $(ui.draggable).draggable("option", "DragType");
    var Captain = document.getElementById(Prefix + "CaptainHiddenField").value;
    var SetPiecesTaker = document.getElementById(Prefix + "SetPiecesTakerHiddenField").value;
    var AreaName;

    if (DragType == "Gridview") {
        HidePlayerIfExists(PlayerId, PositionName);
    }

    // show dropped player here
    document.getElementById(Prefix + PositionName + "PlayerIdHiddenField").value = PlayerId;
    document.getElementById(Prefix + PositionName + "InjuredHiddenField").value = IsInjured;
    document.getElementById(Prefix + PositionName + "PlayerName").innerHTML = PlayerName;
    $("#" + PositionName + "Dragable").draggable("option", "playerId", PlayerId);
    $("#" + PositionName + "Dragable").draggable("option", "playerName", PlayerName);
    $("#" + PositionName + "Dragable").draggable("option", "IsInjured", IsInjured);
    $("#" + PositionName).removeClass("DroppableDivStyleNoBG");
    $("#" + PositionName).addClass("DroppableDivStyle");

    // deal with Captain
    if (PlayerId == Captain) {
        // dropped a captain here - removing the captain
        RemoveCaptains();
        document.getElementById(Prefix + "CaptainHiddenField").value = "0";        
    }

    // deal with set pieces taker
    if (PlayerId == SetPiecesTaker) {
        // dropped a set pieces taker here
        RemoveSetPiecesTakers();
        document.getElementById(Prefix + "SetPiecesTakerHiddenField").value = "0"; 
    }

    // read draggable Area name - will be needed for operations 
    // when other draggable from droppable is dropped.
    AreaName = $(ui.draggable).draggable("option", "AreaName");
    if (CurrentPlayerId != "0") {
        if (DragType == "Gridview") {
            // show current player in the gridview
            ShowPlayerRow(CurrentPlayerId);

            // hide dropped player from the gridview
            HidePlayerRow(PlayerId);

            // Show dropped player in the Penalties area
            ShowPlayerInPenaltiesArea(PlayerId, PlayerName);

            // remove current player from Player OUT and Player In DDLs
            RemovePlayerFromPlayerInDDL(CurrentPlayerId);
            RemovePlayerFromPlayerOutDDL(CurrentPlayerId);

            // add dropped player to player in and player Out DDLs
            AddPlayerToPlayerInDDL(PlayerId, PlayerName);
            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);            

            // hide previous player from the penalties area
            HidePlayerFromPenaltiesArea(CurrentPlayerId);
        }
        else {
            // swap happens:
            // 1. show current player there
            document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value = CurrentPlayerId;
            document.getElementById(Prefix + AreaName + "InjuredHiddenField").value = CurrentInjured;
            document.getElementById(Prefix + AreaName + "PlayerName").innerHTML = CurrentPlayerName;
            $(ui.draggable).draggable("option", "playerId", CurrentPlayerId);
            $(ui.draggable).draggable("option", "playerName", CurrentPlayerName);
            $(ui.draggable).draggable("option", "IsInjured", CurrentInjured);

            if (!IsTrueSubDraggable(DragType)) {
                // remove current player form Player In DDl
                RemovePlayerFromPlayerInDDL(CurrentPlayerId);
                // add dropped player to playerInDDL
                AddPlayerToPlayerInDDL(PlayerId, PlayerName);                
            }
        }
        // update grid rows
        UpdatePlayerPositionImage(PlayerId);
        UpdatePlayerPositionImage(CurrentPlayerId);
    }
    else {
        if (DragType == "Gridview") {
            //hide dropped player from the gridview
            HidePlayerRow(PlayerId);

            // Show dropped player in the Penalties area
            ShowPlayerInPenaltiesArea(PlayerId, PlayerName);

            // add the player to Player in and player Out sub DDLs
            AddPlayerToPlayerInDDL(PlayerId, PlayerName);
            AddPlayerToPlayerOutDDL(PlayerId, PlayerName);
        }
        else {
            // hide dropped player there
            document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value = 0;
            document.getElementById(Prefix + AreaName + "InjuredHiddenField").value = "false";
            document.getElementById(Prefix + AreaName + "PlayerName").innerHTML = "";
            $(ui.draggable).draggable("option", "playerId", 0);
            $(ui.draggable).draggable("option", "playerName", "");
            $(ui.draggable).draggable("option", "IsInjured", "false");

            $("#" + $(ui.draggable).draggable("option", "AreaName")).removeClass("DroppableDivStyle");
            $("#" + $(ui.draggable).draggable("option", "AreaName")).addClass("DroppableDivStyleNoBG");

            // close divs there:
            $("#" + AreaName + "Dragable").hide(200);
            $("#" + AreaName + "Instructions").hide(200);

            // if dropped a player from field - reduce number of field players
            if (!IsSubDraggable(DragType)) {
                var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
                HiddneField.value = parseInt(HiddneField.value) - 1;
            }

            // deal with Substitution DDLs
            if (!IsTrueSubDraggable(DragType)) {
                AddPlayerToPlayerInDDL(PlayerId, PlayerName);
            }

            // update Grid position
            UpdatePlayerPositionImage(PlayerId);
        }

        // open divs here:
        $("#" + PositionName + "Dragable").fadeIn(200);
        $("#" + PositionName + "Instructions").fadeIn(200);
    }
    ShowFormation();
    PlayerInGridOnMouseOut(PlayerId);
    if (CurrentPlayerId != "0") {
        PlayerInGridOnMouseOut(CurrentPlayerId);    
    }
}

function CaptainDivDrop(AreaName, event, ui) {
    RemoveCaptains();
    document.getElementById(AreaName + "CaptainImageDiv").className = "CaptainDivStyle";
    document.getElementById(Prefix + "CaptainHiddenField").value = document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value;
}

function SetPiecesDrop(AreaName, event, ui) {
    RemoveSetPiecesTakers();
    document.getElementById(AreaName + "SetPiecesTakerImageDiv").className = "SetPiecesTakerDivStyle";
    document.getElementById(Prefix + "SetPiecesTakerHiddenField").value = document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value;
}

//function RemoveFieldPlayer(AreaName) {
//    var PlayerId = document.getElementById(Prefix + AreaName + "PlayerIdHiddenField").value;
//    var DragType = "#" + AreaName + "Dragable";

//    CheckDroppedOnPlayersGrid(PlayerId);
//    if (!IsSubDraggable(DragType)) {

//        var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
//        HiddneField.value = parseInt(HiddneField.value) - 1;
//    }
//    // Captain and set pieces takers:
//    var Captain = document.getElementById(Prefix + "CaptainHiddenField");
//    if (Captain.value == PlayerId) {
//        Captain.value = "0";
//        RemoveCaptains();
//    }
//    var SetPiecesTaker = document.getElementById(Prefix + "SetPiecesTakerHiddenField");
//    if (SetPiecesTaker.value == PlayerId) {
//        SetPiecesTaker.value = "0";
//        RemoveSetPiecesTakers();
//    }
//    alert("going to hide");
//    // hide previous player from the penalties area
//    HidePlayerFromPenaltiesArea(PlayerId);

//    // hide player from substitute DDLS
//    RemovePlayerFromPlayerOutDDL(PlayerId);
//    if (IsTrueSubDraggable(DragType)) {
//        RemovePlayerFromPlayerInDDL(PlayerId);
//    }
//    ShowFormation();
//}

$(document).ready(function () {
    $("#" + Prefix + "PlayersGridView").tablesorter();
    ShowFormation();
    ShowCaptain();
    ShowSetPiecesTaker();
    InitPenaltyTakersLists();
    InitPlayerInAndOutDDL();
    InitSubArrows();
    var PenTakers = document.getElementById(Prefix + "PenaltiesVisibleHiddenField").value;
    if (PenTakers == "0") {
        //$("#PenaltiesDiv").hide();
        $("#" + Prefix + "ShowPenaltyTakersButton").hide();
    }
});

$(function () {

    $("#LeftAttack").droppable({
        activeClass: "ActiveDroppableStyle",
        hoverClass: "HoverDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftAttack", event, ui);
        }
    });
    $("#LeftAttack").disableSelection();

    $("#LeftCenterAttack").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftCenterAttack", event, ui);
        }
    });
    $("#LeftCenterAttack").disableSelection();

    $("#CenterAttack").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("CenterAttack", event, ui);
        }
    });
    $("#CenterAttack").disableSelection();

    $("#RightCenterAttack").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightCenterAttack", event, ui);
        }
    });
    $("#RightCenterAttack").disableSelection();

    $("#RightAttack").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightAttack", event, ui);
        }
    });
    $("#RightAttack").disableSelection();

    $("#LeftMidfield").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftMidfield", event, ui);
        }
    });
    $("#LeftMidfield").disableSelection();

    $("#LeftCenterMidfield").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftCenterMidfield", event, ui);
        }
    });
    $("#LeftCenterMidfield").disableSelection();

    $("#CenterMidfield").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("CenterMidfield", event, ui);
        }
    });
    $("#CenterMidfield").disableSelection();

    $("#RightCenterMidfield").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightCenterMidfield", event, ui);
        }
    });
    $("#RightCenterMidfield").disableSelection();

    $("#RightMidfield").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightMidfield", event, ui);
        }
    });
    $("#RightMidfield").disableSelection();

    $("#LeftDefence").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftDefence", event, ui);
        }
    });
    $("#LeftDefence").disableSelection();

    $("#LeftCenterDefence").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("LeftCenterDefence", event, ui);
        }
    });
    $("#LeftCenterDefence").disableSelection();

    $("#CenterDefence").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("CenterDefence", event, ui);
        }
    });
    $("#CenterDefence").disableSelection();

    $("#RightCenterDefence").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightCenterDefence", event, ui);
        }
    });
    $("#RightCenterDefence").disableSelection();

    $("#RightDefence").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("RightDefence", event, ui);
        }
    });
    $("#RightDefence").disableSelection();

    $("#Keeper").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            FieldDroppableDrop("Keeper", event, ui);
        }
    });
    $("#Keeper").disableSelection();

    $("#Sub1").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            SubDroppableDrop("Sub1", event, ui);
        }
    });
    $("#Sub1").disableSelection();

    $("#Sub2").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            SubDroppableDrop("Sub2", event, ui);
        }
    });
    $("#Sub2").disableSelection();

    $("#Sub3").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            SubDroppableDrop("Sub3", event, ui);
        }
    });
    $("#Sub3").disableSelection();

    $("#Sub4").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            SubDroppableDrop("Sub4", event, ui);
        }
    });
    $("#Sub4").disableSelection();

    $("#Sub5").droppable({
        hoverClass: "HoverDroppableStyle",
        activeClass: "ActiveDroppableStyle",
        drop: function (event, ui) {
            SubDroppableDrop("Sub5", event, ui);
        }
    });
    $("#Sub5").disableSelection();

    $("#playersGrid").droppable({
        activeClass: "PlayersDroppableActiveStyle",
        hoverClass: "PlayersDroppableHoverStyle",
        drop: function (event, ui) {
            var PlayerId = $(ui.draggable).draggable("option", "playerId");
            var DragType = $(ui.draggable).draggable("option", "DragType");
            var AreaName = $(ui.draggable).draggable("option", "AreaName");

            // hide the players background from the firld:
            $("#" + AreaName).removeClass("DroppableDivStyle");
            $("#" + AreaName).addClass("DroppableDivStyleNoBG");

            CheckDroppedOnPlayersGrid(PlayerId);
            if (!IsSubDraggable(DragType)) {

                var HiddneField = document.getElementById(Prefix + "NumPlayersHiddenField");
                HiddneField.value = parseInt(HiddneField.value) - 1;
            }

            // Captain and set pieces takers:
            var Captain = document.getElementById(Prefix + "CaptainHiddenField");
            if (Captain.value == PlayerId) {
                Captain.value = "0";
                RemoveCaptains();
            }
            var SetPiecesTaker = document.getElementById(Prefix + "SetPiecesTakerHiddenField");
            if (SetPiecesTaker.value == PlayerId) {
                SetPiecesTaker.value = "0";
                RemoveSetPiecesTakers();
            }

            // hide previous player from the penalties area
            HidePlayerFromPenaltiesArea(PlayerId);

            // hide player from substitute DDLS
            RemovePlayerFromPlayerOutDDL(PlayerId);
            if (IsTrueSubDraggable(DragType)) { RemovePlayerFromPlayerInDDL(PlayerId); }

            ShowFormation();
            PlayerInGridOnMouseOut(PlayerId);
        }
    });

    $("#LeftAttackDragable").draggable({
        opacity: 0.80,
        refreshPositions: true,
        containment: "#DragContainer",
        stack: "#LeftAttackDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftAttackPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftAttackPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftAttackPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftAttackInjuredHiddenField").value,
        DragType: "#LeftAttackDragable",
        AreaName: "LeftAttack",
        start: function () {
            $("#LeftAttack").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftAttack").droppable("option", "accept", "*");
        }
    });
    $("#LeftCenterAttackDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterAttackDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftCenterAttackPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftCenterAttackPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftCenterAttackPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftCenterAttackInjuredHiddenField").value,
        DragType: "#LeftCenterAttackDragable",
        AreaName: "LeftCenterAttack",
        start: function () {
            $("#LeftCenterAttack").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftCenterAttack").droppable("option", "accept", "*");
        }
    });
    $("#CenterAttackDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterAttackDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "CenterAttackPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "CenterAttackPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "CenterAttackPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "CenterAttackInjuredHiddenField").value,
        DragType: "#CenterAttackDragable",
        AreaName: "CenterAttack",
        start: function () {
            $("#CenterAttack").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#CenterAttack").droppable("option", "accept", "*");
        }
    });
    $("#RightCenterAttackDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterAttackDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightCenterAttackPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightCenterAttackPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightCenterAttackPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightCenterAttackInjuredHiddenField").value,
        DragType: "#RightCenterAttackDragable",
        AreaName: "RightCenterAttack",
        start: function () {
            $("#RightCenterAttack").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightCenterAttack").droppable("option", "accept", "*");
        }
    });

    $("#RightAttackDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightAttackDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightAttackPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightAttackPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightAttackPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightAttackInjuredHiddenField").value,
        DragType: "#RightAttackDragable",
        AreaName: "RightAttack",
        start: function () {
            $("#RightAttack").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightAttack").droppable("option", "accept", "*");
        }
    });
    $("#LeftMidfieldDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftMidfieldDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftMidfieldPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftMidfieldPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftMidfieldPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftMidfieldInjuredHiddenField").value,
        DragType: "#LeftMidfieldDragable",
        AreaName: "LeftMidfield",
        start: function () {
            $("#LeftMidfield").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftMidfield").droppable("option", "accept", "*");
        }
    });
    $("#LeftCenterMidfieldDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterMidfieldDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftCenterMidfieldPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftCenterMidfieldPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftCenterMidfieldPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftCenterMidfieldInjuredHiddenField").value,
        DragType: "#LeftCenterMidfieldDragable",
        AreaName: "LeftCenterMidfield",
        start: function () {
            $("#LeftCenterMidfield").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftCenterMidfield").droppable("option", "accept", "*");
        }
    });
    $("#CenterMidfieldDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterMidfieldDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "CenterMidfieldPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "CenterMidfieldPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "CenterMidfieldPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "CenterMidfieldInjuredHiddenField").value,
        DragType: "#CenterMidfieldDragable",
        AreaName: "CenterMidfield",
        start: function () {
            $("#CenterMidfield").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#CenterMidfield").droppable("option", "accept", "*");
        }
    });
    $("#RightCenterMidfieldDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterMidfieldDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightCenterMidfieldPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightCenterMidfieldPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightCenterMidfieldPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightCenterMidfieldInjuredHiddenField").value,
        DragType: "#RightCenterMidfieldDragable",
        AreaName: "RightCenterMidfield",
        start: function () {
            $("#RightCenterMidfield").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightCenterMidfield").droppable("option", "accept", "*");
        }
    });
    $("#RightMidfieldDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightMidfieldDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightMidfieldPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightMidfieldPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightMidfieldPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightMidfieldInjuredHiddenField").value,
        DragType: "#RightMidfieldDragable",
        AreaName: "RightMidfield",
        start: function () {
            $("#RightMidfield").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightMidfield").droppable("option", "accept", "*");
        }
    });
    $("#LeftDefenceDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftDefenceDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftDefencePlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftDefencePlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftDefencePlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftDefenceInjuredHiddenField").value,
        DragType: "#LeftDefenceDragable",
        AreaName: "LeftDefence",
        start: function () {
            $("#LeftDefence").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftDefence").droppable("option", "accept", "*");
        }
    });
    $("#LeftCenterDefenceDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterDefenceDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "LeftCenterDefencePlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "LeftCenterDefencePlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "LeftCenterDefencePlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "LeftCenterDefenceInjuredHiddenField").value,
        DragType: "#LeftCenterDefenceDragable",
        AreaName: "LeftCenterDefence",
        start: function () {
            $("#LeftCenterDefence").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#LeftCenterDefence").droppable("option", "accept", "*");
        }
    });
    $("#CenterDefenceDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterDefenceDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "CenterDefencePlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "CenterDefencePlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "CenterDefencePlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "CenterDefenceInjuredHiddenField").value,
        DragType: "#CenterDefenceDragable",
        AreaName: "CenterDefence",
        start: function () {
            $("#CenterDefence").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#CenterDefence").droppable("option", "accept", "*");
        }
    });
    $("#RightCenterDefenceDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterDefenceDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightCenterDefencePlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightCenterDefencePlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightCenterDefencePlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightCenterDefenceInjuredHiddenField").value,
        DragType: "#RightCenterDefenceDragable",
        AreaName: "RightCenterDefence",
        start: function () {
            $("#RightCenterDefence").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightCenterDefence").droppable("option", "accept", "*");
        }
    });
    $("#RightDefenceDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightDefenceDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "RightDefencePlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "RightDefencePlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "RightDefencePlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "RightDefenceInjuredHiddenField").value,
        DragType: "#RightDefenceDragable",
        AreaName: "RightDefence",
        start: function () {
            $("#RightDefence").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#RightDefence").droppable("option", "accept", "*");
        }
    });
    $("#KeeperDragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#KeeperDragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "KeeperPlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "KeeperPlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "KeeperPlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "KeeperInjuredHiddenField").value,
        DragType: "#KeeperDragable",
        AreaName: "Keeper",
        start: function () {
            $("#Keeper").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Keeper").droppable("option", "accept", "*");
        }
    });
    $("#Sub1Dragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#Sub1Dragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "Sub1PlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "Sub1PlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "Sub1PlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "Sub1InjuredHiddenField").value,
        DragType: "#Sub1Dragable",
        AreaName: "Sub1",
        start: function () {
            $("#Sub1").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Sub1").droppable("option", "accept", "*");
        }
    });
    $("#Sub2Dragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#Sub2Dragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "Sub2PlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "Sub2PlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "Sub2PlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "Sub2InjuredHiddenField").value,
        DragType: "#Sub2Dragable",
        AreaName: "Sub2",
        start: function () {
            $("#Sub2").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Sub2").droppable("option", "accept", "*");
        }
    });
    $("#Sub3Dragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#Sub3Dragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "Sub3PlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "Sub3PlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "Sub3PlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "Sub3InjuredHiddenField").value,
        DragType: "#Sub3Dragable",
        AreaName: "Sub3",
        start: function () {
            $("#Sub3").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Sub3").droppable("option", "accept", "*");
        }
    });
    $("#Sub4Dragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#Sub4Dragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "Sub4PlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "Sub4PlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "Sub4PlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "Sub4InjuredHiddenField").value,
        DragType: "#Sub4Dragable",
        AreaName: "Sub4",
        start: function () {
            $("#Sub4").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Sub4").droppable("option", "accept", "*");
        }
    });
    $("#Sub5Dragable").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#Sub5Dragable",
        helper: function () {
            var dom = [];
            var playerName = document.getElementById(Prefix + "Sub5PlayerName").innerHTML;
            dom.push("<div class=\"playerdraggablehelper\">" + playerName + "</div>");
            return $(dom.join(''));
        },
        revert: "invalid",
        playerId: document.getElementById(Prefix + "Sub5PlayerIdHiddenField").value,
        playerName: document.getElementById(Prefix + "Sub5PlayerName").innerHTML,
        IsInjured: document.getElementById(Prefix + "Sub5InjuredHiddenField").value,
        DragType: "#Sub5Dragable",
        AreaName: "Sub5",
        start: function () {
            $("#Sub5").droppable("option", "accept", "none");
        },
        stop: function () {
            $("#Sub5").droppable("option", "accept", "*");
        }
    });


    // captain and set pieces takeres dragabls and dropabls
    //    $("#Captain").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            var PlayerId = $(ui.draggable).draggable("option", "playerId");
    //            var PlayerName = $(ui.draggable).draggable("option", "playerName").replace("<br /> ", '\n');

    //            // set Captain to be the dropped player. Need nothing else.
    //            document.getElementById(Prefix + "CaptainIdHiddenField").value = PlayerId;
    //            document.getElementById(Prefix + "CaptainLabel").innerHTML = PlayerName;
    //            $("#CaptainDraggable").draggable("option", "playerId", PlayerId);
    //            $("#CaptainDraggable").draggable("option", "playerName", PlayerName);
    //            $("#CaptainDraggable").fadeIn(200);
    //        }
    //    });
    //    $("#Captain").disableSelection();

    //    $("#SetPiecesTaker").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            var PlayerId = $(ui.draggable).draggable("option", "playerId");
    //            var PlayerName = $(ui.draggable).draggable("option", "playerName").replace("<br /> ", '\n');

    //            // set Captain to be the dropped player. Need nothing else.
    //            document.getElementById(Prefix + "SetPiecesTakerIdHiddenField").value = PlayerId;
    //            document.getElementById(Prefix + "SetPiecesTakerLabel").innerHTML = PlayerName;
    //            $("#SetPiecesTakerDraggable").draggable("option", "playerId", PlayerId);
    //            $("#SetPiecesTakerDraggable").draggable("option", "playerName", PlayerName);
    //            $("#SetPiecesTakerDraggable").fadeIn(200);
    //        }
    //    });
    //    $("#SetPiecesTaker").disableSelection();

    //    $("#CaptainDraggable").draggable({
    //        opacity: 0.80,
    //        containment: "#DragContainer",
    //        stack: "#CaptainDraggable",
    //        helper: "clone",
    //        revert: "invalid",
    //        playerId: document.getElementById(Prefix + "CaptainIdHiddenField").value,
    //        playerName: document.getElementById(Prefix + "CaptainLabel").innerHTML,
    //        DragType: "#CaptainDraggable",
    //        AreaName: "CaptainDraggable",
    //        start: function() {
    //            DisableFieldDroppables();
    //        },
    //        stop: function() {
    //            EnableFieldDroppables();
    //        }
    //    });

    //    $("#SetPiecesTakerDraggable").draggable({
    //        opacity: 0.80,
    //        containment: "#DragContainer",
    //        stack: "#SetPiecesTakerDraggable",
    //        helper: "clone",
    //        revert: "invalid",
    //        playerId: document.getElementById(Prefix + "SetPiecesTakerIdHiddenField").value,
    //        playerName: document.getElementById(Prefix + "SetPiecesTitleLabel").innerHTML,
    //        DragType: "#SetPiecesTakerDraggable",
    //        AreaName: "SetPiecesTakerDraggable",
    //        start: function() {
    //            DisableFieldDroppables();
    //        },
    //        stop: function() {
    //            EnableFieldDroppables();
    //        }
    //    });

    //    $("#PenaltyGridViewDroppable").droppable({
    //        activeClass: "PlayersDroppableActiveStyle",
    //        hoverClass: "PlayersDroppableHoverStyle",
    //        drop: function(event, ui) {
    //            var droppedPlayerId = $(ui.draggable).draggable("option", "playerId");
    //            var droppedPenaltyNum = $(ui.draggable).draggable("option", "PenaltyNum");

    //            // show the player here
    //            ShowPenaltyPlayersGridRow(droppedPlayerId);

    //            // hide the player there:
    //            HidePenaltyDraggable(droppedPenaltyNum);
    //        }
    //    });

    //    $("#Penalty1Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "1");
    //        }
    //    });
    //    $("#Penalty2Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "2");
    //        }
    //    });
    //    $("#Penalty3Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "3");
    //        }
    //    });
    //    $("#Penalty4Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "4");
    //        }
    //    });
    //    $("#Penalty5Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "5");
    //        }
    //    });
    //    $("#Penalty6Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "6");
    //        }
    //    });
    //    $("#Penalty7Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "7");
    //        }
    //    });
    //    $("#Penalty8Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "8");
    //        }
    //    });
    //    $("#Penalty9Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "9");
    //        }
    //    });
    //    $("#Penalty10Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "10");
    //        }
    //    });
    //    $("#Penalty11Droppable").droppable({
    //        hoverClass: "HoverDroppableStyle",
    //        activeClass: "ActiveDroppableStyle",
    //        drop: function(event, ui) {
    //            PenaltyDroppableDrop(event, ui, "11");
    //        }
    //    });

    $("#MasterCaptainDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#MasterCaptainDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#MasterSetPiecesDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#MasterSetPiecesDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#LeftAttackCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftAttackCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftAttackSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftAttackSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#LeftCenterAttackCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterAttackCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftCenterAttackSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterAttackSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }

    });

    $("#CenterAttackCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterAttackCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#CenterAttackSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterAttackSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#RightCenterAttackCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterAttackCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightCenterAttackSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterAttackSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#RightAttackCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightAttackCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightAttackSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightAttackSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#LeftMidfieldCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftMidfieldCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftMidfieldSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftMidfieldSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#LeftCenterMidfieldCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterMidfieldCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftCenterMidfieldSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterMidfieldSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#CenterMidfieldCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterMidfieldCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#CenterMidfieldSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterMidfieldSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });
    $("#RightCenterMidfieldCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterMidfieldCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightCenterMidfieldSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterMidfieldSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });
    $("#RightMidfieldCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightMidfieldCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightMidfieldSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightMidfieldSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });
    $("#LeftDefenceCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftDefenceCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftDefenceSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftDefenceSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#LeftCenterDefenceCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterDefenceCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#LeftCenterDefenceSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#LeftCenterDefenceSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#CenterDefenceCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterDefenceCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#CenterDefenceSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#CenterDefenceSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });
    $("#RightCenterDefenceCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterDefenceCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightCenterDefenceSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightCenterDefenceSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#RightDefenceCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightDefenceCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#RightDefenceSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#RightDefenceSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });

    $("#KeeperCaptainImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#KeeperCaptainImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "Captain",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersCaptainDroppables();
        },
        stop: function () {
            EnableEmptyPlayersCaptainDroppables();
        }

    });

    $("#KeeperSetPiecesTakerImageDiv").draggable({
        opacity: 0.80,
        containment: "#DragContainer",
        stack: "#KeeperSetPiecesTakerImageDiv",
        helper: "clone",
        revert: "invalid",
        scope: "SetPieces",
        refreshPositions: true,
        start: function () {
            DisableEmptyPlayersSetPiecesDroppables();
        },
        stop: function () {
            EnableEmptyPlayersSetPiecesDroppables();
        }
    });


    $("#LeftAttackCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftAttack", event, ui);
        }
    });
    $("#LeftAttackSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",

        drop: function (event, ui) {
            SetPiecesDrop("LeftAttack", event, ui)
        }
    });
    $("#LeftCenterAttackCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftCenterAttack", event, ui);
        }
    });
    $("#LeftCenterAttackSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        tolerance: "pointer",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("LeftCenterAttack", event, ui);
        }
    });
    $("#CenterAttackCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("CenterAttack", event, ui);
        }
    });
    $("#CenterAttackSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("CenterAttack", event, ui);
        }
    });
    $("#RightCenterAttackCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightCenterAttack", event, ui);
        }
    });
    $("#RightCenterAttackSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightCenterAttack", event, ui);
        }
    });
    $("#RightAttackCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightAttack", event, ui);
        }
    });
    $("#RightAttackSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightAttack", event, ui);
        }
    });
    $("#LeftMidfieldCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftMidfield", event, ui);
        }
    });
    $("#LeftMidfieldSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("LeftMidfield", event, ui);
        }
    });
    $("#LeftCenterMidfieldCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftCenterMidfield", event, ui);
        }
    });
    $("#LeftCenterMidfieldSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("LeftCenterMidfield", event, ui);
        }
    });
    $("#CenterMidfieldCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("CenterMidfield", event, ui);
        }
    });
    $("#CenterMidfieldSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("CenterMidfield", event, ui);
        }
    });
    $("#RightCenterMidfieldCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightCenterMidfield", event, ui);
        }
    });
    $("#RightCenterMidfieldSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightCenterMidfield", event, ui);
        }
    });
    $("#RightMidfieldCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightMidfield", event, ui);
        }
    });
    $("#RightMidfieldSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightMidfield", event, ui);
        }
    });
    $("#LeftDefenceCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftDefence", event, ui);
        }
    });
    $("#LeftDefenceSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("LeftDefence", event, ui);
        }
    });
    $("#LeftCenterDefenceCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("LeftCenterDefence", event, ui);
        }
    });
    $("#LeftCenterDefenceSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("LeftCenterDefence", event, ui);
        }
    });
    $("#CenterDefenceCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("CenterDefence", event, ui);
        }
    });
    $("#CenterDefenceSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("CenterDefence", event, ui);
        }
    });
    $("#RightCenterDefenceCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightCenterDefence", event, ui);
        }
    });
    $("#RightCenterDefenceSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightCenterDefence", event, ui);
        }
    });
    $("#RightDefenceCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("RightDefence", event, ui);
        }
    });
    $("#RightDefenceSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("RightDefence", event, ui);
        }
    });
    $("#KeeperCaptainImageDiv").droppable({
        scope: "Captain",
        activeClass: "ActiveCaptainStyle",
        hoverClass: "HoverCaptainStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            CaptainDivDrop("Keeper", event, ui);
        }
    });
    $("#KeeperSetPiecesTakerImageDiv").droppable({
        scope: "SetPieces",
        activeClass: "ActiveSetPiecesStyle",
        hoverClass: "HoverSetPiecesStyle",
        tolerance: "touch",
        drop: function (event, ui) {
            SetPiecesDrop("Keeper", event, ui);
        }
    });

})
function HidePositionDraggable(Position, PlayerId) {
    var Positions = new Array();
    Positions[0] = "LeftAttack";
    Positions[1] = "LeftCenterAttack";
    Positions[2] = "CenterAttack";
    Positions[3] = "RightCenterAttack";
    Positions[4] = "RightAttack";
    Positions[5] = "LeftMidfield";
    Positions[6] = "LeftCenterMidfield";
    Positions[7] = "CenterMidfield";
    Positions[8] = "RightCenterMidfield";
    Positions[9] = "RightMidfield";
    Positions[10] = "LeftDefence";
    Positions[11] = "LeftCenterDefence";
    Positions[12] = "CenterDefence";
    Positions[13] = "RightCenterDefence";
    Positions[14] = "RightDefence";
    Positions[15] = "Keeper";
    Positions[16] = "Sub1";
    Positions[17] = "Sub2";
    Positions[18] = "Sub3";
    Positions[19] = "Sub4";
    Positions[20] = "Sub5";
    var HiddenFieldName;
                  
    for (i = 0; i <= 20; i++) {
        if (Positions[i] != Position) {
            HiddenFieldName = Prefix + Positions[i] + "PlayerIdHiddenField";
            if (document.getElementById(HiddenFieldName).value == PlayerId) {
                document.getElementById(HiddenFieldName).value = 0;
                document.getElementById(Prefix + Positions[i] + "InjuredHiddenField").value = "false";
                document.getElementById(Prefix + Positions[i] + "PlayerName").innerHTML = "";
                $("#" + Positions[i] + "Dragable").draggable("option", "playerId", 0);
                $("#" + Positions[i] + "Dragable").draggable("option", "playerName", "");
                $("#" + Positions[i] + "Dragable").draggable("option", "IsInjured", "false");

                $("#" + Positions[i] + "Dragable").hide();
                $("#" + Positions[i] + "Instructions").hide();
                
            }
        }
    }
}

function HideColumn(num) {
    rows = document.getElementById(Prefix + "PlayersGridView").rows;
    for (i = 0; i < rows.length; i++) {
        $(rows[i].cells[num]).hide();
    }

}

function ShowColumn(num) {
    rows = document.getElementById(Prefix + "PlayersGridView").rows;
    for (i = 0; i < rows.length; i++) {
        $(rows[i].cells[num]).fadeIn(200);
    }

}

function CheckDroppedOnPlayersGrid(PlayerId) {
    HidePositionDraggable("None", PlayerId);
    ShowPlayerRow(PlayerId);
}

function ShowHidePlayersColumns(CheckBoxName) {
    var ColumnNumber;

    if (CheckBoxName == "AgeCheckBox") {
        ColumnNumber = 7;
    }
    else if (CheckBoxName == "CharismaCheckBox") {
        ColumnNumber = 8;
    }
    else if (CheckBoxName == "ExperienceCheckBox") {
        ColumnNumber = 6;
    }
    else if (CheckBoxName == "StrongFootCheckBox") {
        ColumnNumber = 9;
    }
    else if (CheckBoxName == "StaminaCheckBox") {
        ColumnNumber = 10;
    }
    else if (CheckBoxName == "InstinctsCheckBox") {
        ColumnNumber = 11;
    }
    else if (CheckBoxName == "GoalKeepingCheckBox") {
        ColumnNumber = 12;
    }
    else if (CheckBoxName == "PositioningCheckBox") {
        ColumnNumber = 13;
    }
    else if (CheckBoxName == "PowerCheckBox") {
        ColumnNumber = 14;
    }
    else if (CheckBoxName == "TacklingCheckBox") {
        ColumnNumber = 15;
    }
    else if (CheckBoxName == "HeadingCheckBox") {
        ColumnNumber = 16;
    }
    else if (CheckBoxName == "QuicknessCheckBox") {
        ColumnNumber = 17;
    }
    else if (CheckBoxName == "TechniqueCheckBox") {
        ColumnNumber = 18;
    }
    else if (CheckBoxName == "ShortPassingCheckBox") {
        ColumnNumber = 19;
    }
    else if (CheckBoxName == "LongPassingCheckBox") {
        ColumnNumber = 20;
    }
    else if (CheckBoxName == "ShootingCheckBox") {
        ColumnNumber = 21;
    }
   // alert("CheckBoxName: " + CheckBoxName + "\nColumnNumber: " + ColumnNumber);
    
    if (document.getElementById(Prefix + CheckBoxName).checked) {
        ShowColumn(ColumnNumber);
    }
    else {
        HideColumn(ColumnNumber);
    }
}

function AttributeChecked(CheckBoxName) {
    var NumCkecked = parseInt(document.getElementById(Prefix + "NumCheckedAttributes").value);
    var alertMessage = document.getElementById(Prefix + "AlertNoteHiddenField").value;
    if (document.getElementById(Prefix + CheckBoxName).checked) {
        if (NumCkecked >= 3) {
            document.getElementById(Prefix + CheckBoxName).checked = false;
            alert(alertMessage);
        }
        else {
            document.getElementById(Prefix + "NumCheckedAttributes").value = NumCkecked + 1;
            ShowHidePlayersColumns(CheckBoxName);
        }
    }
    else {
        document.getElementById(Prefix + "NumCheckedAttributes").value = NumCkecked - 1;
        ShowHidePlayersColumns(CheckBoxName);
    }
}


function GetRBLSelectedValue(RBLName) {
    var IdPrefix = Prefix;
    var NamePrefix = "ctl00$ctl00$ContentPlaceHolder3$ContentPlaceHolder1$";                       
    var List = document.getElementById(IdPrefix + RBLName);
    var Elements = document.getElementsByName(NamePrefix + RBLName);
    var Selected = -1;
    var SelectedValue = 0;
    
    for (var i = 0; i < Elements.length; i++) {
        if (Elements[i].checked) {
            Selected = i;
        }
    }
    if (Selected >= 0) { SelectedValue = Elements[Selected].value; }
    return SelectedValue;
}

function ValidatePlayerInstruction(position) {
//    var PlayerId;
//    var Instruction;
//    var PreferredAction;
//    PlayerId = document.getElementById(Prefix + position + "PlayerIdHiddenField").value;
//    if (PlayerId != "0") {
//        if (position == "Keeper") {
//            Instruction == "2";
//        }
//        else {
//            Instruction = GetRBLSelectedValue("Instructions" + position + "RadioButtonList");
//        }
//        PreferredAction = GetRBLSelectedValue("PreferredAction" + position + "RadioButtonList");

//        if (Instruction == "0" || PreferredAction == "0") {
//            alert("Position: " + position + ", Instruction: " + Instruction + ", PreferredAction: " + PreferredAction);
//            return false;
//        }
//    }
    // currently no validation on field player instruction or preferred action is needed. It is always populated (change from previous design)
    // 
    return true;
}

function ValidateSubPoisitions(position) {
    var PlayerId;
    var SubPosition;
    
    PlayerId = document.getElementById(Prefix + position + "PlayerIdHiddenField").value;
    if (PlayerId != "0") {
        SubPosition = GetRBLSelectedValue(position + "PositionsRadioButtonList");
        if (SubPosition == "0") {
            return false;
        }
    }
    return true;

}

function CheckCaptainOnSquad() {
    var Postfix = "PlayerIdHiddenField";
    var CaptainId = document.getElementById(Prefix + "CaptainHiddenField").value;
    if (CaptainId == "0") { return false; }
    if (document.getElementById(Prefix + "LeftAttack" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterAttack" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "CenterAttack" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightCenterAttack" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightAttack" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "LeftMidfield" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterMidfield" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "CenterMidfield" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightCenterMidfield" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightMidfield" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "LeftDefence" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterDefence" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "CenterDefence" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightCenterDefence" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "RightDefence" + Postfix).value == CaptainId) { return true; }
    if (document.getElementById(Prefix + "Keeper" + Postfix).value == CaptainId) { return true; }


    return false;
}

function CheckSetPiecesTakerOnSquad() {
    var Postfix = "PlayerIdHiddenField";
    var SetPiecesTakerId = document.getElementById(Prefix + "SetPiecesTakerHiddenField").value;
    if (SetPiecesTakerId == "0") { return false; }
    if (document.getElementById(Prefix + "LeftAttack" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterAttack" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "CenterAttack" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightCenterAttack" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightAttack" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "LeftMidfield" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterMidfield" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "CenterMidfield" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightCenterMidfield" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightMidfield" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "LeftDefence" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "LeftCenterDefence" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "CenterDefence" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightCenterDefence" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "RightDefence" + Postfix).value == SetPiecesTakerId) { return true; }
    if (document.getElementById(Prefix + "Keeper" + Postfix).value == SetPiecesTakerId) { return true; }


    return false;
}

function ValidateDDLSOnSubmit() {
    // verifies that where a player is located - he has location and instruction
    // verifies that the captain and the set pieces taker are on the squad
    // veifies that Aggressiveness and Attitude values are set

    // step1: Assign message names
    var PleaseAssignAllInstructionsMessage = document.getElementById(Prefix + "AssignAllInstructionsMessageHiddenField").value;
    var PleaseSetTeamAttitudeMessage = document.getElementById(Prefix + "NeedAttitudeMEssageHiddenField").value;
    var PleaseSetTeamAggressivenessMessage = document.getElementById(Prefix + "NeedAggressivenessMessageHiddenField").value;
    var PleaseAssignAllSubPositionsMessage = document.getElementById(Prefix + "ChooseSubPositionsMessageHiddenField").value;
    var CaptainMustBeOnTheSquadMessage = document.getElementById(Prefix + "CaptainMustBeMessageHiddenField").value;
    var SetPiecesTakerMustBeOnTheSquadMessage = document.getElementById(Prefix + "SetPiecesTakerMustBeMessageHiddenField").value;
    var SubmitOrdersConfirmMessage = document.getElementById(Prefix + "AreYouSureSubmitOrdersHiddenField").value;
    
    // check that where field player instructions are assigned where playerId is not 0.
    if (!ValidatePlayerInstruction("LeftAttack")) {
        alert(PleaseAssignAllInstructionsMessage); 
        return false;
    }
    if (!ValidatePlayerInstruction("LeftCenterAttack")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("CenterAttack")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightCenterAttack")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightAttack")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("LeftMidfield")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("LeftCenterMidfield")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("CenterMidfield")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightCenterMidfield")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightMidfield")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("LeftDefence")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("LeftCenterDefence")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("CenterDefence")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightCenterDefence")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("RightDefence")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }
    if (!ValidatePlayerInstruction("Keeper")) {
        alert(PleaseAssignAllInstructionsMessage);
        return false;
    }

    // check that substitute players have positions
    for (var i = 1; i<=5; i++){
        if (!ValidateSubPoisitions("Sub"+i)) {
            alert(PleaseAssignAllSubPositionsMessage);
            return false;
        }
    }

    // check that team attitude is set
    var TeamAttitude = document.getElementById(Prefix + "TeamAttitudeDropDownList").value;
    if (TeamAttitude == "0") {
        alert(PleaseSetTeamAttitudeMessage);
        return false;
    }

    // check that team aggressiveness is set
    var TeamAggressiveness = document.getElementById(Prefix + "TeamAggressivenessDropDownList").value;
    if (TeamAggressiveness == "0") {
        alert(PleaseSetTeamAggressivenessMessage);
        return false;
    }

    // check that captain is one of the starting players
    if (!CheckCaptainOnSquad()) {
        alert(CaptainMustBeOnTheSquadMessage);
        return false;
    }
    // check that set pieces taker is one of the starting players
    if (!CheckSetPiecesTakerOnSquad()) {
        alert(SetPiecesTakerMustBeOnTheSquadMessage);
        return false;
    }

    // asking the user whether he wishes to submit the orders.
    if (!confirm(SubmitOrdersConfirmMessage)) {
        return false;
    }

    // if came to here - returning that everything is OK.
    return true;
}


function ListItemClick(ListName, ItemText) {
    // this fuction is called each time radio button is clicked
    // its purpose is to change the text of the correspondent label
    document.getElementById(ListName + "Label").innerHTML = ItemText;    
}
